<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/21 0021
 * Time: 上午 10:02
 */

namespace Home\Controller;

use Think\Controller;

class OrderController extends Controller
{
    public function add(){
        //如果会员没有登录就跳到登录页面，登陆成功后再跳回这个页面
        $memberId = session('m_id');
        if(!$memberId){
            //先登录成功之后要跳回的地址存到session
            session('returnUrl',U('Order/add'));
            redirect(U('Member/login'));
        }
        //var_dump(I('post.cart'));exit;
        //var_dump(I('post.'));exit;
       if(IS_POST){
            $orderModel = D('Admin/Order');
            if($orderModel->create(I('post.'),1)){
                if($orderId = $orderModel->add()){
                    $this->success('下单成功!',U('order_success?order_id='.$orderId));
                    exit;
                }
            }
            $this->error($orderModel->getError());
        }

        $model = D('Admin/UserAddress');
        $info = $model->relation(true)->where( array( 'is_default' => 1, 'member_id' => $memberId ) )->find();
        $all_info = $model->search();
        $cartModel = D('Admin/Cart');
        $data =$cartModel->cartList();
        //$amount=I('post.amount');
        //取出送货方式
        $shipingModel = M('shipping');
        $shippingData = $shipingModel->where(array('enabled'=>array('eq',1)))
            ->order('shipping_order desc,shipping_id asc')->select();
        //取出发票信息
        $inv_config  = C('INV_CONTENT');
        $this->assign(array(
            'all_info'=>$all_info['data'],
            'info' =>$info,
            /*'amount'=>$amount,*/
            'shippingData'=>$shippingData,
            'data'=>$data,
            'inv_config' => $inv_config,
            '_page_title'=>'订单确认页',
            '_page_keywords'=>'订单确认页',
            '_page_description'=>'订单确认页'
        ));
        $this->display();
    }
    public function receive(){
        require('./alipay/notify_url.php');
    }
    public function test(){
        $db =M();
        $rs =$db->query('SELECT id FROM test.a');
        $rs[0]['id']--;
        $db->execute('UPDATE test.a SET id='.$rs[0]['id']);
    }
    public function order_success(){
        $btn =makeAlipayBtn(I('get.order_id'));
        $this->assign(array(
            'btn'=>$btn,
            '_page_title'=>'下单成功',
            '_page_keywords'=>'下单成功',
            '_page_description'=>'下单成功'
        ));
        $this->display();
    }
    function edit(){
        if(IS_POST){
            $model = D('Order');
            if($model->create(I('post.'),2)){
                if($model->save() !== FALSE){
                    $this->success('订单更新成功',U('lst',array('p'=>I('get.p',1))));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $orderId = I('get.order_id');
        $orderModel = D('Admin/Order');
        $orderData = $orderModel->getDetail($orderId);
        $this->assign('order',$orderData);
        $this->assign(array(
           'page_title' =>'订单更新',
            '_page_btn_name'=>'列表',
            '_page_btn_link'=>U('lst')
        ));
        $this->display();
    }
}